package com.mxspace.rpc.component;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * RPC异步线程池
 */
@EnableAsync
@Configuration
public class RpcTaskPoolConfig {

    /**
     * 线程池
     * @return
     */
    @Bean("mxRpcExecutor")
    public Executor taskExecutor(){
        //线程池核数
        int i = Runtime.getRuntime().availableProcessors();
        ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
        //核心线程池大小
        threadPoolExecutor.setCorePoolSize(16);
        //最大线程数
        threadPoolExecutor.setMaxPoolSize(20);
        //队列容量
        threadPoolExecutor.setQueueCapacity(9999);
        //活跃时间
        threadPoolExecutor.setKeepAliveSeconds(60);
        //线程池名字前缀
        threadPoolExecutor.setThreadGroupName("MxRpc-Async-Task");
        //应用关闭最大秒数
        threadPoolExecutor.setAwaitTerminationMillis(60);
        //等待所有线程结束在关闭线程池
        threadPoolExecutor.setWaitForTasksToCompleteOnShutdown(true);
        return threadPoolExecutor;
    }
}
